package com.cf.user.permission;

import com.cf.user.permission.filter.PermissionFilter;
import com.cf.user.permission.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * game-server-UserSysConfig
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/5/28 下午 07:20
 */
@Slf4j
@EnableConfigurationProperties(PermissionProperty.class)
public class PermissionConfig extends WebMvcConfigurerAdapter {
    public PermissionConfig() {
        log.info("实例化PermissionConfig");
    }

    //#####################权限########################################


    @Bean
    @ConditionalOnMissingBean(PermissionManager.class)
    public PermissionManager permissionManager() {
        log.info("未找到自定义的PermissionManager，实例化默认的DefaultPermissionManagerImpl");
        return new DefaultPermissionManagerImpl();
    }

    @Bean
    @ConditionalOnMissingBean(SystemPermissionService.class)
    public SystemPermissionService systemPermissionService() {
        log.info("未找到自定义的PermissionService，实例化默认的SystemPermissionServiceImpl");
        return new SystemPermissionServiceImpl();
    }

    @Bean
    @ConditionalOnMissingBean(SystemRoleServiceImpl.class)
    public SystemRoleServiceImpl systemRoleService() {
        log.info("未找到自定义的systemRoleService，实例化默认的SystemRoleServiceImpl");
        return new SystemRoleServiceImpl();
    }

    @Bean
    public PermissionFilter permissionFilter() {
        log.info("实例化PermissionFilter");
        return new PermissionFilter();
    }

    @Bean
    public FilterRegistrationBean permissionFilterRegistrationBean(PermissionFilter permissionFilter) {
        log.info("注册permissionFilter");
        FilterRegistrationBean myFilter = new FilterRegistrationBean();
        myFilter.addUrlPatterns(permissionFilter.matchPathPatterns().toArray(new String[]{}));
        myFilter.setOrder(permissionFilter.getOrder());
        myFilter.setFilter(permissionFilter);
        return myFilter;
    }


}
